using System;
using System.Collections;
using System.Data;
using TSoft.Data;


namespace Coffee.Lists
{
	public class clsLST_Supplier : iControls.IBaseClass
	{

		#region Prop
		public System.Int32 SupplierID
		{
			 get;
			 set;
		}

		public System.String Code
		{
			 get;
			 set;
		}

		public System.String Name
		{
			 get;
			 set;
		}

		public System.String Address
		{
			 get;
			 set;
		}

		public System.String Tel
		{
			 get;
			 set;
		}

		public System.String Notes
		{
			 get;
			 set;
		}

		#endregion

		#region  Constructors
		public clsLST_Supplier()
		{
		}

		public clsLST_Supplier(System.Int32 v_SupplierID)
		{
			LoadData(v_SupplierID);
		}

		#endregion

		#region Methods
		public override void ResetData()
		{
			SupplierID = Int32.MinValue;
			Code = string.Empty;
			Name = string.Empty;
			Address = string.Empty;
			Tel = string.Empty;
			Notes = string.Empty;
		}

		public void LoadData(System.Int32 v_SupplierID)
		{
			DataTable dt = new DataTable();
			try
			{
				dt = DBLayer.ExecDataTable("SP_LST_Supplier", " Command,SupplierID", "LoadData", v_SupplierID);
				this.ResetData();
				if(dt==null || dt.Rows.Count<=0)
					return;
				this.FillData(dt.Rows[0]);
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}

		public override void FillData(DataRow row)
		{
			ResetData();
			if(row.Table.Columns.Contains("SupplierID"))
				SupplierID = TSoft.Data.ValueType.GetValue(row["SupplierID"], SupplierID);
			if(row.Table.Columns.Contains("Code"))
				Code = TSoft.Data.ValueType.GetValue(row["Code"], Code);
			if(row.Table.Columns.Contains("Name"))
				Name = TSoft.Data.ValueType.GetValue(row["Name"], Name);
			if(row.Table.Columns.Contains("Address"))
				Address = TSoft.Data.ValueType.GetValue(row["Address"], Address);
			if(row.Table.Columns.Contains("Tel"))
				Tel = TSoft.Data.ValueType.GetValue(row["Tel"], Tel);
			if(row.Table.Columns.Contains("Notes"))
				Notes = TSoft.Data.ValueType.GetValue(row["Notes"], Notes);
		}

		public override void OnUpdate()
		{
			Hashtable output = null;
			string s_ParamName = "";
			object[] o_ParamValue = null;
			try
			{
				TSoft.Data.ParamHolder.ClearAll();
				TSoft.Data.ParamHolder.AddParam("Command", "Update");
				TSoft.Data.ParamHolder.AddParam("SupplierID", SupplierID);
				TSoft.Data.ParamHolder.AddParam("Code", Code);
				TSoft.Data.ParamHolder.AddParam("Name", Name);
				TSoft.Data.ParamHolder.AddParam("Address", Address);
				TSoft.Data.ParamHolder.AddParam("Tel", Tel);
				TSoft.Data.ParamHolder.AddParam("Notes", Notes);
				TSoft.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
				DBLayer.ExecNonQuery("SP_LST_SUPPLIER", s_ParamName, ref output, o_ParamValue);
			}
			catch( Exception ex)
			{
				throw ex;
			}
		}

		public override void OnDelete()
		{
			string s_ParamName = "";
			object[] o_ParamValue = null;
			try
			{
				TSoft.Data.ParamHolder.ClearAll();
				TSoft.Data.ParamHolder.AddParam("Command", "Delete");
				TSoft.Data.ParamHolder.AddParam("SupplierID", SupplierID);
				TSoft.Data.ParamHolder.GetParams(ref s_ParamName,ref o_ParamValue);
                DBLayer.ExecNonQuery("usp_LST_Supplier", s_ParamName, o_ParamValue);
			}
			catch( Exception ex)
			{
				throw ex;
			}
		}


		#endregion

        #region Developer
        public DataTable GetAll()
        {
            try
            {
                return DBLayer.ExecDataTable("usp_LST_Supplier", "Command", "GetAll");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Update(System.Data.DataTable Data)
        {
            try
            {
                Hashtable output = null;
                DBLayer.BeginTransaction();

                foreach (DataRow row in Data.Rows)
                {
                    if (row.RowState == DataRowState.Added || row.RowState == DataRowState.Modified)
                    {
                        DBLayer.ExecNonQuery("usp_LST_Supplier", "Command,SupplierID,Code,Name,Address,Tel,Notes", ref output, "Update",
                            TSoft.Cvert.ToInt32(row["SupplierID"], 0),TSoft.Cvert.ToString(row["Code"], "")
                            , TSoft.Cvert.ToString(row["Name"], ""), TSoft.Cvert.ToString(row["Address"], "")
                            , TSoft.Cvert.ToString(row["Tel"], ""), TSoft.Cvert.ToString(row["Notes"], ""));
                        if (!object.Equals(output, null) && output.Count > 0)
                            row["SupplierID"] = TSoft.Cvert.ToInt32(output["PositionID"], 0);
                    }
                }

                DBLayer.CommitTransaction();
            }
            catch(Exception ex)
            {
                DBLayer.RollbackTransaction();
                throw ex;
            }
        }
        #endregion
    }
}
